// 创建用户相关的小仓库
import { defineStore } from "pinia";
import type { majorAllResponseData, majorInfoData, majorListFormData, majorListResponseData, majorResponseData } from "@/api/major/type";
import { reqMajorAdd, reqMajorAll, reqMajorById, reqMajorDelete, reqMajorList, reqMajorUpdate } from "@/api/major";
// 引入state返回对象类型
import type { MajorState } from "./types/type";
// 创建专业小仓库
let useMajorStore = defineStore('Major',{
    // 这里使用选择式Api写法
    // 存储数据位置
    // 指定state函数返回值类型为MajorState
    state:() : MajorState=>{
        return{
            majors:[]
        }
    },
    // 异步|逻辑
    actions:{
        // 获取所有专业
        async getMajorAll(){
            let result:majorAllResponseData = await reqMajorAll();
            if(result.status == 0){
                this.majors = (result.data as majorInfoData[])
                // 当前async函数返回一个成功的promise
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 分页获取专业
        async getMajorList(data:majorListFormData){
            let result:majorListResponseData = await reqMajorList(data);
            if(result.status == 0){
                this.majors = result.data?.data as majorInfoData[]
                // 当前async函数返回一个成功的promise
                return result.data
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 添加专业
        async addMajor(data:majorInfoData){
            let result:majorResponseData = await reqMajorAdd(data);
            if(result.status == 0){
                // 当前async函数返回一个成功的promise
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 根据id查询专业
        async getMajorById(_id:string){
            let result:majorResponseData = await reqMajorById(_id);
            if(result.status == 0){
                // 当前async函数返回一个成功的promise
                return result.data
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 更新专业信息
        async updateMajor(data:majorInfoData){
            let result:majorResponseData = await reqMajorUpdate(data);
            if(result.status == 0){
                // 当前async函数返回一个成功的promise
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 删除专业
        async deleteMajor(majorId:string){
            let result:majorResponseData = await reqMajorDelete(majorId);
            if(result.status == 0){
                // 当前async函数返回一个成功的promise
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
    },
    // 计算属性
    getters:{

    }
})
// 对外暴露获取小仓库方法
export default useMajorStore